import { HttpError, toSuccess, useTransaction } from "@sker/core"
import { useData } from "@sker/runtime"
import { AutoDevice } from "@sker/entities"
import { z } from "zod"

export default async () => {
    const input = z.object({
        token: z.string(),
        settings: z.any()
    })
    const body = input.parse(useData());
    await useTransaction(async m => {
        const device = await m.findOne(AutoDevice, { where: { token: body.token } });
        if (!device) throw new HttpError(`设备不存在或已删除`);
        // 保存配置 生成定时任务
        await m.update(AutoDevice, device.id, { settings: body.settings });
        // 通知客户端 开始工作
    })
    return toSuccess(true)
}